﻿#if UNITY_OPENHARMONY
using System;
using UnityEngine;
using UnityEngine.OpenHarmony;

public class OpenHarmonyWebview
{
    private event Action WebviewAction = null;
    public event Action<string> OnError;
    public event Action<string> OnHttpError;
    Webview web;

    private void callback(string str)
    {
        Debug.LogError("" + str);
    }

    public OpenHarmonyWebview()
    {
       
    }

    public void CreateWebview(int ml,int mt,int mr,int mb,Boolean visible, Action webviewAction)
    {
        Debug.Log("call CreateWebview");
        WebviewAction = webviewAction;
        web = new Webview(ml, mt, mr, mb, visible);
        WebviewAction();
        web.OnError += callback;
        web.OnHttpError += callback;
    }

    public void RemoveWebview()
    {
        Debug.Log("call RemoveWebview");
        web.Delete();
        web = null;
        WebviewAction = null;
    }

    public void LoadURL(string url)
    {
        Debug.Log("call LoadURL");
        web.Url = url;
    }


    public void LoadHTMLString(string contents, string baseUrl)
    {
        Debug.Log("call LoadHTMLString");
        web.LoadHTMLString(contents,baseUrl);
    }
    
    public void LoadData(string contents, string mimeType, string encoding, string baseUrl)
    {
        Debug.Log("call LoadData");
        web.LoadData(contents, mimeType, encoding, baseUrl);
    }
    
    public void EvaluateJS(string jsContents)
    {
        Debug.Log("call EvaluateJS");
        web.EvaluateJS(jsContents);
    }

    public void SetVisibility(Boolean visible)
    {
        Debug.Log("call SetVisibility");
        web.Visible = visible;
    }

    public void SetMargins(int ml, int mt, int mr, int mb)
    {
        Debug.Log("call SetMargins");
        web.SetMargins(ml,mt,mr,mb);
    }

    public void Reload()
    {
        Debug.Log("call Reload");
        web.Reload();
    }

    public void StopLoading()
    {
        Debug.Log("call StopLoading");
        web.StopLoading();
    }

    public void GoForward()
    {
        Debug.Log("call GoForward");
        web.GoForward();
    }

    public void GoBack()
    {
        Debug.Log("call GoBack");
        web.GoBack();
    }
}
#endif